@isTest
public class QuoteDealerControllerTest
{
    static testMethod void QuoteDealerControllerTest()
    {
        
 		Id ldcProfile = [Select Id From Profile Where Name ='系统管理员' or Name='System Administrator'].Id;
        User u1 = new User( Alias = 'samLdc1', 
                         Email = 'ldc@test.com.maike',
                         Emailencodingkey = 'UTF-8', 
                         Lastname = '用户1', 
                         Languagelocalekey = 'en_US',
                         Localesidkey = 'en_US', 
                         Profileid = ldcProfile, 
                         Country = 'United States',
                         IsActive = true,
                         SubCompany__c='四川省迈克实业有限公司',
                         UserDepartment__c='客户服务中心(工程部)',
                         Timezonesidkey = 'America/Los_Angeles', 
                         EmployeeNumber = '123451',
                         Username = 'Matldc1@test.com.maike' );
        insert u1;
       
        
        // 客户
        Account acc = TestUtils.createAccount();
        acc.BillingType__c = '专票';
        insert acc;
		
        
        // 业务机会
        Opportunity opp = TestUtils.createOpportunity();
        opp.AccountId = acc.Id;
        insert opp;
        
		ProductStructure__c ps2 = new ProductStructure__c();
        ps2.Classify3__c = '试剂';
        insert ps2;
        // 产品
        Product__c p = TestUtils.createProduct();
        p.bggsjhdj__c = 10.0;
        p.zl__c = '12.5ml';
        p.ProductStructure__c = ps2.Id;
        
        Product__c p2 = TestUtils.createProduct();
        p2.Name = 'aaaa';
        p2.bggsjhdj__c = 10.0;
        p2.zl__c = '100ml';
        p2.ProductStructure__c = ps2.Id;
        
        Product__c p3 = TestUtils.createProduct();
        p3.Name = '测试合同产品';
        p3.bggsjhdj__c = 10.0;
        p3.zl__c = '200ml';
        p3.ProductStructure__c = ps2.Id;
        insert new Product__c[] {p, p2,p3};

      

        // 价格
        Price__c price = TestUtils.createPrice();
        price.Product__c = p.Id;
        price.Company__c = '四川省迈克实业有限公司';
        price.InstCost__c = 10.0;
        price.InvoiceDealerPrice__c = 10.0;
        price.Taxrate__c = 0.03;
        price.MarketPrice__c = 10.0;
        
        Price__c price2 = TestUtils.createPrice();
        price2.Product__c = p2.Id;
        price2.Company__c = '四川省迈克实业有限公司';
        price2.InstCost__c = 10.0;
        price2.InvoiceDealerPrice__c = 10.0;
        price2.Taxrate__c = 0.17;
        price2.MarketPrice__c = 10.0;
        
        Price__c price3 = TestUtils.createPrice();
        price3.Product__c = p3.Id;
        price3.Company__c = '四川省迈克实业有限公司';
        price3.InstCost__c = 10.0;
        price3.InvoiceDealerPrice__c = 10.0;
        price3.Taxrate__c = 0.17;
        price3.MarketPrice__c = 10.0;
        insert new Price__c[] {price, price2,price3};
        
            /// 日期处理
        Integer m = Date.today().month(); //当前月份
        Integer y = Date.today().year();  //当前年份 
       ///取上月
         if(m == 1){
        m = 12 ;
            y -= 1;
        }else{
        m -= 1;
            
        }
        
        String  month = String.valueOf(m) + '月';
        String  year= String.valueOf(y) + '年';
        AveragePrice__c av1 = new AveragePrice__c(
        Product__c = p.Id,
            Company__c = '四川省迈克实业有限公司',
            AveragePrice__c = 1.0,
            Year__c = year,
            Month__c = month
            
        );
          AveragePrice__c av2 = new AveragePrice__c(
       		 Product__c = p2.Id,
            Company__c = '四川省迈克实业有限公司',
            AveragePrice__c = 1.0,
            Year__c = year,
            Month__c = month
            
        );
        insert new AveragePrice__c[] {av2,av1};
        
        ///合同
        Contract__c c = new Contract__c();
        c.name = '测试合同';
        c.Account__c = acc.Id;
        c.YearContract__c = String.valueOf(Date.today().year()) + '年经销商年度合同';
        insert c;
        
        ///合同产品
        ContractProduct__c  cp = new ContractProduct__c();
        cp.Contract__c = c.Id;
        cp.Product__c = p3.Id;
        insert cp;

        // ---------------------新建-----------------------
        PageReference page = new PageReference('/apex/QuoteDealer?oppid=' + opp.Id );
        System.Test.setCurrentPage(page);
        QuoteDealerController qc = new QuoteDealerController(new ApexPages.StandardController(new Quote()));
		qc.u = u1;
        qc.init();
        System.assertEquals(0, qc.pbList.size());
        //System.assertEquals(1, qc.qpbList.size());

        qc.name = 'aaaa';
        qc.brand = '济南希森美康';
        qc.serial = 'ZG801009';
        qc.category0 = '试剂';
        qc.category1 = '临检';
        qc.search();

        System.assertEquals(1, qc.pbList.size());

        qc.pbList[0].chk = true;
        qc.exchange();
        
        qc.shaixuan();
        qc.cancelOption();
        qc.shaixuan();
        qc.saveOption();
        qc.shaixuan();
        for(QuoteDealerController.QuoteProductBean qp : qc.tempList){
        	qp.chk = false;
        }
        qc.deleteOption();
        System.assertEquals(2, qc.qpbList.size());
        System.Test.startTest();
        qc.save1();
        
        List<Quote> q = [select Id from Quote where OpportunityId = :opp.Id];
        System.assertEquals(1, q.size());
                // ---------------------编辑-----------------------
        page = new PageReference('/apex/QuoteDealer?qid=' +q[0].Id);
        System.Test.setCurrentPage(page);
       QuoteDealerController qc1 = new QuoteDealerController(new ApexPages.StandardController(q[0]));
		qc1.u = u1;
        qc1.init();
        

    }
}